# -*- coding:utf-8 -*-
from django.shortcuts import render, redirect, reverse
from django.views.generic.base import View
from pure_pagination import Paginator, PageNotAnInteger

from .models import CommunityBanner, Topic, TopicReply
from users.models import Advertisement
from .forms import TopicForm, TopicReplyForm


class CommunityView(View):
    def get(self, request):
        topic_form = TopicForm()
        banners = CommunityBanner.objects.all().order_by('index')
        topics = Topic.objects.all().order_by('-add_time')
        ad_image = Advertisement.objects.all()
        hot_topics = Topic.objects.all().order_by('-comment_number')[:5]
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(topics, 5, request=request)
        topics = p.page(page)
        return render(request, 'community.html', {
            'banners': banners,
            'topics': topics,
            'ad_image': ad_image,
            'hot_topics': hot_topics,
            'topic_form': topic_form,
        })

    def post(self, request):
        if not request.user.is_authenticated():
            return redirect(reverse('login'))
        topic_form = TopicForm(request.POST)
        if topic_form.is_valid():
            topic = Topic()
            topic.title = request.POST.get('title')
            topic.user = request.user
            topic.content = request.POST.get('content')
            topic.save()
        return redirect(reverse('community:community_list'))


class CommunityDetailView(View):
    def get(self, request, topic_id):
        topic_reply_form = TopicReplyForm()
        topic = Topic.objects.get(id=int(topic_id))
        topic.click_number += 1
        topic.save()
        topic_replys = TopicReply.objects.filter(topic=int(topic_id)).order_by('add_time')
        ad_image = Advertisement.objects.all()
        hot_topics = Topic.objects.all().order_by('-comment_number')[:5]
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(topic_replys, 5, request=request)
        topic_replys = p.page(page)
        return render(request, 'community-detail.html', {
            'topic': topic,
            'topic_replys': topic_replys,
            'ad_image': ad_image,
            'hot_topics': hot_topics,
            'topic_reply_form': topic_reply_form,
        })

    def post(self, request, topic_id):
        if not request.user.is_authenticated():
            return redirect(reverse('login'))
        topic_reply_form = TopicReplyForm(request.POST)
        if topic_reply_form.is_valid():
            topic_reply = TopicReply()
            topic_reply.topic_id = int(topic_id)
            topic_reply.user = request.user
            topic_reply.content = request.POST.get('content')
            topic_reply.save()
            topic = Topic.objects.get(id=int(topic_id))
            topic.comment_number += 1
            topic.save()
        return redirect(reverse('community:community_detail', kwargs={'topic_id': topic_id}))
